<template>
    <div class="file_list_content">
        <vxe-table-template :data="fileList" :options="tableOptions" />
        <el-dialog title="查看" :visible.sync="fileVisible"
                   :append-to-body="true" width="600px" v-dialogDrag>
            <div class="dialogHead">
                <CustomMedia :options="mediaOptions" />
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="fileVisible = false">关闭</el-button>
            </div>
        </el-dialog>

        <el-dialog title="选择打印机" :visible.sync="chooseVisible"
                   :append-to-body="true" width="500px" v-dialogDrag>
            <div class="dialog_content">
                <el-form ref="form" label-width="100px">
                    <el-form-item label="选择打印机" style="margin-bottom: 0;">
                        <el-select v-model="printer" style="width: 95%;" v-if="GSCloudPlugin">
                            <el-option v-for="(item, index) in GSCloudPlugin.Printers" :key="index" :label="item"
                                       :value="item" />
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <a target="_blank" href="/printerSetup.exe" style="float:left">下载打印驱动</a>
                <el-button type="primary" @click="handleConfirmPrint">确认打印</el-button>
                <el-button @click="chooseVisible = false">关闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import fileApi from "@common/api/fileApi";
import CustomMedia from "../common/CustomMedia";
import print from "@common/utils/print";
import { download } from '@common/utils/download';

export default {
    name: "FileList",
    components: { CustomMedia },
    props: {
        options: {
            type: Object,
            default() {
                return {};
            }
        },
        list: {
            type: Array,
            default() {
                return [];
            },
        },
        edit: {
            type: Boolean,
            default: false,
        },
        print: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        let _this = this;
        return {
            GSCloudPlugin: undefined,
            printer: '',
            chooseVisible: false,
            fileApiUrl: fileApi.url,
            fileList: [],
            account_set_sid: _this.$utils.getAccountSetSid(),
            tableOptions: {
                ref: 'file_list_table',
                size: 'mini',
                columns: [
                    {
                        label: '附件名称',
                        prop: 'name',
                        align: 'center',
                    }
                ],
                operates: {
                    count: _this.edit && _this.print ? 4 : (!_this.edit && !_this.print ? 2 : 3),
                    list: [
                        {
                            id: 'show',
                            // label: '查看',
                            type: 'primary',
                            icon: 'el-icon-search',
                            size: 'mini',
                            show: true,
                            tooltip: {
                                content: '查看',
                            },
                            method: (index, row) => {
                                _this.viewFileDetail(row);
                            }
                        },
                        {
                            id: 'export',
                            // label: '下载',
                            type: 'info',
                            icon: 'el-icon-download',
                            size: 'mini',
                            show: true,
                            tooltip: {
                                content: '下载',
                            },
                            method: (index, row) => {
                                _this.downloadFileDetail(row);
                            }
                        },
                        {
                            id: 'delete',
                            // label: '删除',
                            type: 'danger',
                            icon: 'fa fa-trash-o',
                            size: 'mini',
                            show: true,
                            tooltip: {
                                content: '删除',
                            },
                            filter() {
                                return _this.edit;
                            },
                            method: (index, row) => {
                                _this.deleteFile(row);
                            }
                        },
                        {
                            id: 'print',
                            // label: '打印',
                            type: 'primary',
                            icon: 'el-icon-printer',
                            size: 'mini',
                            show: true,
                            tooltip: {
                                content: '打印',
                            },
                            filter() {
                                return _this.print;
                            },
                            method: (index, row) => {
                                _this.handleClickPrint(row);
                            },
                        },
                    ],
                },
            },
            fileVisible: false,
            mediaOptions: {},
            currRow: {},
        };
    },
    watch: {
        options: {
            handler() {
                this.tableOptions = $.extend({}, this.tableOptions, this.options);
            },
            deep: true,
            immediate: true,
        },
        list: {
            handler() {
                this.initList();
            },
            deep: true,
            immediate: true,
        },
    },
    methods: {
        initList() {
            this.fileList = [].concat(this.list);
            this.fileList.forEach((item, index) => {
                item._index = index;
                item.ratio = 0;
            });
        },
        viewFileDetail(row) {
            let $this = this;
            fileApi.getFileInfo(row.sid, result => {
                if (result.data) {
                    let resData = result.data;
                    // let url = resData.file_path;
                    let url = $this.getFileWithType(resData.sid, resData.file_type);
                    let officePreviewType = '';
                    if (resData.file_type.toLowerCase().indexOf('pdf') !== -1) {
                        officePreviewType = 'pdf';
                    }
                    $this.$utils.readFile(url, officePreviewType);
                }
            })
        },
        downloadFileDetail(row) {
            let _this = this;
            let url = `${ _this.fileApiUrl.getFile }${ _this.account_set_sid }/${ row.sid }`;
            download({ url, name: row.original_file_name });
        },
        deleteFile(row) {
            let $this = this;
            $this.$confirm("是否继续删除？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let list = this.fileList.filter(item => item._index !== row._index);
                this.fileList = list;
                this.$emit('handleDelFile', list);
            }).catch(() => {
            });
        },
        handleClickPrint(row) {
            if (this.GSCloudPlugin) {
                this.currRow = $.extend({}, row);
                if (!this.$utils.isNotEmpty(this.printer)) {
                    this.printer = this.GSCloudPlugin.Printers[0];
                }
                this.chooseVisible = true;
            } else {
                this.$message.error('无法连接到光速云插件服务，请确认光速云插件是否已安装或已启动');
            }
        },
        handleConfirmPrint() {
            this.GSCloudPlugin.PrinterName = this.printer;
            this.chooseVisible = false;
            this.printFile(this.currRow);
        },
        printFile(row) {
            let $this = this;
            print.showLoading(`正在打印${ row.name }.....`);
            print.printFile(this.GSCloudPlugin, row.sid, row.name).then(res => {
                print.hideLoading();
                console.log(res);
            }).catch(err => {
                print.hideLoading();
                if (err.Code === 201) {
                    $this.$message.info(err.Message);
                } else {
                    $this.$message.error(err.Message);
                }
            });
        }
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.file_list_content {
}
</style>
